package qnb.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import qnb._common._ModuleKey;
import qnb._common.easyui.EasyuiGridData;
import qnb._common.easyui.EasyuiPageVO;
import qnb._common.tools.StringTool;
import qnb._common.vo.MessageVOFactory;
import qnb.entity.Course1;
import qnb.entity.Course2;
import qnb.entity.Grade;
import qnb.service.Course12Service;
import qnb.service.GradeService;
import weixinkeji.vip.expand.poi.JWEOfficeWebTool;

@Controller
@RequestMapping("/course")
public class Course12Controller {
	
	@Autowired
	private Course12Service service;
    @Autowired
    private GradeService gradeService;
	// 跳转到添加视图（jsp页面）
	@RequestMapping("/forward/add")
	public String forwardTAdd() {
		return "course/add";
	}
	//跳转到  修改视图
	@RequestMapping("/forward/update")
	public String forwardToUpdate(Integer id,HttpServletRequest req) {
		if (null == id || id < 0) {
			return null;
		}
		
		Course1 obj=service.select1ById(id);
		if(null==obj||null==obj.getCourse1Id()) {
			return null;
		}
		
		req.setAttribute("Course1Object",obj);
		return "course/update";
	}
	// 跳转到查询视图（jsp页面）
	@RequestMapping("/forward/list")
	public String forwardList() {
		return "course/list";
	}
	
//===============第三方模块交互使用==============================================================
    @PostMapping("/selectAllGradeName")
    @ResponseBody
    public Object selectAllGradeName(){
        List<Grade> listGradeName = gradeService.selectAllGradeName();
        return listGradeName;
    }

//=============================================================================
	// 添加
	@RequestMapping("/add")
	@ResponseBody
	public String add(Course1 headObject,String bodyJsonData) {
		System.out.println(headObject.getGradeName());
		//从表单分离出：表体数据
		List<Course2> list=StringTool.jsonStringToList(Course2.class, bodyJsonData);
		System.out.println(list.isEmpty());
		return MessageVOFactory.getAddMessage(service.insertCourse(headObject, list));
	}

	// 删除
	@RequestMapping("/dell")
	@ResponseBody
	public String dell(Integer id) {
		if (null == id || id < 0) {
			return null;
		}
		return MessageVOFactory.getDellMessage(service.dellCourse(id));
	}

	// 修改
	@RequestMapping("/update")
	@ResponseBody
	public String update(Course1 headObject,String bodyJsonData) {
		//从表单分离出：表体数据
		List<Course2> list=StringTool.jsonStringToList(Course2.class, bodyJsonData);
		return MessageVOFactory.getUpdateMessage(service.updateCourse(headObject, list));
	}

	// 查询一个科目对象
	// 目前 结合修改使用
	@RequestMapping("/one")
	@ResponseBody
	public Object selectOne(Integer id) {
		if (null == id || id < 0) {
			return null;
		}
		return 1;
	}

	// 表头：分页查询 
	@RequestMapping("/list1")
	@ResponseBody
	public Object list(EasyuiPageVO vo) {
		if (vo.isSqlError()) {
			return null;
		}
		String whereSql = vo.getWhereSql(_ModuleKey.Course1);
		System.out.println("前端传来的条件："+whereSql);
		vo.setSort("course1_id");
		vo.setOrder("desc");
		
		return EasyuiGridData.getEasyuiGridData(service.select1Count(whereSql),
				service.select(whereSql, vo.getOrderbySQL(), vo.getPageSQL(), vo.getRows()));
	}
	// 表体：查询
	@RequestMapping("/list2")
	@ResponseBody
	public Object list2(HttpServletResponse resp ,Integer id) throws ServletException, IOException {
		if(null==id) {
			return "[]";
		}
		List<Course2> list=service.select2ByCourse1Id(id);
		return EasyuiGridData.getEasyuiGridData(list.size(),list);
	}
	
	// 表体：查询与下载
	@RequestMapping("/list2/down")
	public void dow(HttpServletResponse resp ,Integer id) throws ServletException, IOException {
		Course1 obj=service.select1ById(id);
		if(null==obj||null==obj.getCourse1Id()) {
			return ;
		}
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		List<Course2> list=service.select2ByCourse1Id(id);
		JWEOfficeWebTool.downloadExcelFile(resp, list, obj.getGradeName()+"课程表("+sf.format(obj.getCourse1Start())+"到"+sf.format(obj.getCourse1End())+").xls", "课程", true);
	}
}
